﻿using dreamwork.mobile.app.api.Core;
using dreamwork.mobile.app.api.Infrastructure;
using dreamwork.mobile.app.api.Models.Data;
using dreamwork.mobile.app.api.Models.ViewModel;
using DereamWork.Model.ViewModel;
using Microsoft.EntityFrameworkCore;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace dreamwork.mobile.app.api.Services
{
    public class TerminalUserService : ITerminalUserService
    {
        private readonly ITerminalUserRepository  _terminalUserRepository;
        private readonly ITokenService _tokenService;
        public TerminalUserService(ITerminalUserRepository terminalUserRepository, ITokenService tokenService)
        {
            _terminalUserRepository = terminalUserRepository;
            _tokenService = tokenService;
        }
        public async Task<UserLoginResult> LoginAsync(TerminalUser user)
        {
            var result = new UserLoginResult
            {
                UserType = UserTypes.TerminalUser
            };
            if (user == null)
            {
                result.LoginResult = false;
                result.Message = "提交信息不完整";
                return result;
            }
            var model = await _terminalUserRepository.GetAsync(x => x.PhoneNumber == user.PhoneNumber);
            if (model == null && !string.IsNullOrEmpty(user.Name))
            {
                model = await _terminalUserRepository.GetAsync(x => x.Name == user.Name.ToLower());
            }
            if (model == null)
            {
                result.LoginResult = false;
                result.Message = "用户不存在";
                return result;
            }
            if (model.Password != user.Password)
            {
                result.LoginResult = false;
                result.Message = "密码不正确";
                return result;
            }
            result.LoginResult = true;
            result.Message = "登录成功";
            result.Token = new UserToken
            {
                Value = _tokenService.CreateToken(model.Id)
            };
            return result;
        }
        public async Task<UserLogoutResult> LogoutAsync()
        {
            var result = new UserLogoutResult()
            {
                UserType = UserTypes.TerminalUser,
                LogoutResult = true
            };
            return await Task.FromResult(result);
        }
        public async Task<ServiceResult<TerminalUser>> GetById(string id)
        {
            var model = await _terminalUserRepository.GetAsync(x => x.Id == id);
            var result = new ServiceResult<TerminalUser>();
            if (model == null)
            {
                result.Message = "信息不存在";
                result.IsSuccess = false;
                return result;
            }
            result.Data = model;
            return result;
        }
        public async Task<ServiceResult<IEnumerable<TerminalUser>>> GetListAsync()
        {
            var models = await _terminalUserRepository.GetAllAsync();
            var result = new ServiceResult<IEnumerable<TerminalUser>>
            {
                Data = models
            };
            return result;
        }
        public async Task<ServiceResult<TerminalUser>> ModifyAsync(TerminalUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            var model = await _terminalUserRepository.GetAsync(x => x.Id == user.Id);
            var result = new ServiceResult<TerminalUser>();
            if (model == null)
            {
                result.Message = "要修改的用户不存在";
                result.IsSuccess = false;
                return result;
            }
            await _terminalUserRepository.UpdateAsync(user);
            result.Data = await _terminalUserRepository.GetAsync(x => x.Id == user.Id);
            return result;
        }
        public async Task<ServiceResult<TerminalUser>> UpdatePositionAsync(TerminalUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var model = await _terminalUserRepository.GetAsync(x => x.Id == user.Id);
            var result = new ServiceResult<TerminalUser>();
            if (model == null)
            {
                result.Message = "要修改的用户不存在";
                result.IsSuccess = false;
                return result;
            }
            model.Position = user.Position;
            model.Address = user.Address;
            await _terminalUserRepository.UpdatePositionAsync(model);
            result.Data = await _terminalUserRepository.GetAsync(x => x.Id == user.Id);
            return result;
        }
    }
}
